﻿using System;
using System.ComponentModel;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using EIIS.Infrastructure.Service.Business;
using EIIS.Infrastructure.Service.System;
using EIIS.Infrastructure.Model.Business;
using EIIS.Infrastructure.Model.System;
using EIIS.Presentation.Controllers.CommonSupport;
using EIIS.Presentation.Controllers.ViewModel;
using EIIS.Common.Jsons;
using EIIS.FrameWork.Attributes;
using EIIS.Infrastructure.Model.System.Authority;

namespace EIIS.Presentation.Controllers.Business
{
    /// <summary>
    /// 包的控制器:灭菌部分
    /// </summary>
    public partial class PackageController : BaseController
    {
        [Description("灭菌管理页面")]
        [ViewPage]
        public ActionResult MIndex()
        {
            return View();
        }

        [Description("灭菌列表页面")]
        [ViewPage]
        public ActionResult MList(string id)
        {
            return View();
        }

        public ActionResult MLoadAllByPage(string id, int page, int rows, string order, string sort)
        {
            long total = 0;
            IList<Package> ilist = GetPackages(id, page, rows, order, sort, out total).OrderByDescending(p => p.MieJSJ).ToList();
            var list = ilist.Select(p => new
            {
                p.Id,
                p.MieJR,
                p.TiaoMH,
                p.ZhuangT,
                p.YouXQ,
                p.ShiXSJ,
                MieJRQ = string.IsNullOrEmpty(p.MieJSJ) == true ? string.Empty : DateTime.Parse(p.MieJSJ).ToString("yyyy-MM-dd"),
                MieJSJ = string.IsNullOrEmpty(p.MieJSJ) == true ? string.Empty : DateTime.Parse(p.MieJSJ).ToString("HH:mm:ss"),
                BaoMC = DictionaryManager.Get(p.BaoMC) == null ? "" : DictionaryManager.Get(p.BaoMC).Name,
                MieJGH = DictionaryManager.Get(p.MieJGH) == null ? "" : DictionaryManager.Get(p.MieJGH).Name,
                MieJGC = DictionaryManager.Get(p.MieJGC) == null ? "" : DictionaryManager.Get(p.MieJGC).Name
            });
            return this.JsonFormat(new GridData { total = total, rows = list.ToList() });
        }

        [Description("新增灭菌")]
        [Function(ButtonID="btnAdd")]
        public ActionResult MAdd()
        {
            this.ViewData["entity"] = new Package();
            return View();
        }

        [Description("确认灭菌")]
        [Function(ButtonID = "btnSave")]
        public ActionResult MSave()
        {
            return View();
        }

        [Description("删除灭菌")]
        [Function(ButtonID = "btnDelete")]
        public ActionResult MDelete(IList<string> idList)
        {           
            if (idList != null)
            {
                PackageManager.Delete(idList.Cast<object>().ToList());
            }
            MessageInfo msg = new MessageInfo();
            msg.IsSuccess = true;
            msg.Message = "删除成功";
            return Json(msg);
        }

        [Description("保存灭菌")]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult MSave(Package entity)
        {
            MessageInfo msg = new MessageInfo();
            if (string.IsNullOrEmpty(entity.Id))//确认灭菌
            {
                string strMieJGH = Request["MieJGH"];
                string strMieJGC = Request["MieJGC"];
                string strYouXQ = Request["YouXQ"];
                string strShiXSJ = Request["ShiXSJ"];
                if (!string.IsNullOrEmpty(strMieJGH)
                    && !string.IsNullOrEmpty(strMieJGC)
                    && !string.IsNullOrEmpty(strYouXQ)
                    && !string.IsNullOrEmpty(strShiXSJ))
                {
                    if (!HasMieJGH(strMieJGH))
                    {
                        msg.IsSuccess = false;
                        msg.Message = "灭菌失败,该锅号不存在";
                        return Json(msg);
                    }
                    else if (!HasMieJGC(strMieJGC))
                    {
                        msg.IsSuccess = false;
                        msg.Message = "灭菌失败,该锅次不存在";
                        return Json(msg);
                    }
                    else if (!HasBaoZQ(strYouXQ))
                    {
                        msg.IsSuccess = false;
                        msg.Message = "灭菌失败,该保质期不存在";
                        return Json(msg);
                    }

                    strShiXSJ = DateTime.Parse(strShiXSJ).ToString("yyyy-MM-dd");
                    if (Cache.GetSessionCache("HospUser") != null)
                    {
                        HospUser user = (HospUser)Cache.GetSessionCache("HospUser");
                        if (!PackageManager.SaveMieJ(strMieJGH, strMieJGC, strYouXQ, strShiXSJ, user.Name))
                        {
                            msg.IsSuccess = false;
                            msg.Message = "灭菌失败!";
                            return Json(msg);
                        }
                    }
                    else 
                    {
                        msg.IsSuccess = false;
                        msg.Message = "获取灭菌人信息失败";
                        return Json(msg);
                    }
                }
                else
                {
                    msg.IsSuccess = false;
                    msg.Message = "获取灭菌锅号等参数失败";
                    return Json(msg);
                }
            }
            else//修改
            {
                var model = PackageManager.Get(entity.Id);
                model.TiaoMH = entity.TiaoMH;
                model.BaoMC = entity.BaoMC;
                model.YouXQ = entity.YouXQ;
                model.MieJGC = entity.MieJGC;
                model.MieJGH = entity.MieJGH;
                model.ShiXSJ = DateTime.Parse(entity.ShiXSJ).ToString("yyyy-MM-dd");
                PackageManager.Update(model);
            }            
            msg.IsSuccess = true;
            msg.Message = "保存成功";
            return Json(msg, "text/html", JsonRequestBehavior.AllowGet);
        }        

        public ActionResult GetDisinfectionNo(string id)
        {
            //灭菌锅号
            IList<Dictionary> ilist = DictionaryManager.LoadAllByCategoryCode("miejgh");
            List<combobox_data> list = ilist.ToList().ConvertAll<combobox_data>(d => { return new combobox_data() { id = d.Id, text = d.Name, selected = (d.Id == id ? true : false) }; });
            return Json(list);
        }

        public ActionResult GetDisinfectionSer(string id)
        {
            //灭菌锅次
            IList<Dictionary> ilist = DictionaryManager.LoadAllByCategoryCode("miejgc");
            List<combobox_data> list = ilist.ToList().ConvertAll<combobox_data>(d => { return new combobox_data() { id = d.Id, text = d.Name, selected = (d.Id == id ? true : false) }; });
            return Json(list);
        }

        public ActionResult GetGuaranteePeriod(string id)
        {
            //保质期
            IList<Dictionary> ilist = DictionaryManager.LoadAllByCategoryCode("baozq");
            List<combobox_data> list = ilist.ToList().ConvertAll<combobox_data>(d => { return new combobox_data() { id = d.Code, text = d.Name, selected = (d.Code == id ? true : false) }; });
            return Json(list);
        }

        //获取包装的包名称
        public ActionResult GetBaoMC(string id)
        {
            Dictionary dictionary = DictionaryManager.GetByTiaoMH(id, 1);
            if (dictionary == null)
            {
                dictionary = new Dictionary();
                dictionary.Id = string.Empty;
                dictionary.Name = string.Empty;
            }
            else
            {
                Package model = PackageManager.GetByTiaoMH(id);
                model.ZhuangT = 20;
                PackageManager.Update(model);
            }
            return this.JsonFormat(new { id = dictionary.Id, text = dictionary.Name });
        }

        /// <summary>
        /// 根据编号效验灭菌锅号是否存在
        /// </summary>
        /// <param name="MieJGH">条目中灭菌锅号对应的ID号</param>
        /// <returns></returns>
        private bool HasMieJGH(string MieJGH)
        {
            var tempList = DictionaryManager.LoadAllByCategoryCode("miejgh").Where(p => p.Id.ToLower() == MieJGH.ToLower());
            int count = tempList.Count();
            return count > 0;
        }

        /// <summary>
        /// 根据编号效验灭菌锅次是否存在
        /// </summary>
        /// <param name="MieJGC">条目中灭菌锅次对应的ID号</param>
        /// <returns></returns>
        private bool HasMieJGC(string MieJGC)
        {
            var tempList = DictionaryManager.LoadAllByCategoryCode("miejgc").Where(p => p.Id.ToLower() == MieJGC.ToLower());
            int count = tempList.Count();
            return count > 0;
        }

        /// <summary>
        /// 根据条目编码效验保质期是否存在
        /// </summary>
        /// <param name="BaoZQ">条目中保质期对应的条目编码</param>
        /// <returns></returns>
        private bool HasBaoZQ(string BaoZQ)
        {
            var tempList = DictionaryManager.LoadAllByCategoryCode("baozq").Where(p => p.Code.ToLower() == BaoZQ.ToLower());
            int count = tempList.Count();
            return count > 0;
        }
    }
}
